/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package testing.jee.web.view;

import com.vaadin.data.validator.AbstractStringValidator;
import testing.jee.util.DurationUtil;

/**
 *
 * @author kreisera
 */
public class DurationValidator extends AbstractStringValidator {

    private int maxDuration = 0;
    private int minDuration = 1;

    public int getMaxDuration() {
        return maxDuration;
    }

    public void setMaxDuration(int maxDuration) {
        this.maxDuration = maxDuration;
    }

    public int getMinDuration() {
        return minDuration;
    }

    public void setMinDuration(int minDuration) {
        this.minDuration = minDuration;
    }
    
    public DurationValidator(String errorMessage) {
        super(errorMessage);
    }

    @Override
    protected boolean isValidString(String value) {
        try {
            int hours = DurationUtil.convertToHours(value);
            if (minDuration > 0 && hours < minDuration) {
                return false;
            } else if (maxDuration > 0 && hours > maxDuration) {
                return false;
            }
        } catch (Exception ex) {
            return false;
        }
        return true;
    }
}
